package com.karaku.karaku_collector.util;

import android.graphics.Bitmap;

import com.karaku.karaku_collector.control.FilterMaskView;

/**
 * Created by guoping on 2017/11/9.
 */

public class WhiteBalance {

    private static final int WIDTH = FilterMaskView.SIZE - UIUtil.dp2px(5);
    private static final int HEIGHT = FilterMaskView.SIZE - UIUtil.dp2px(5);

    public static WhiteBalanceResult getWhiteBalanceResult(Bitmap bmp) {
        int[] pixels = new int[WIDTH * HEIGHT];
        bmp.getPixels(pixels, 0, WIDTH, (bmp.getWidth() - WIDTH) / 2, (bmp.getHeight() - HEIGHT) / 2, WIDTH, HEIGHT);
        long r = 0, g = 0, b = 0;
        long total = 0;
        for (int i = 0; i < pixels.length; ++i) {
            int p = pixels[i];
            r += ((p >>> 16) & 0XFF);
            g += ((p >>> 8) & 0XFF);
            b += (p & 0XFF);
            total += (r + g + b);
        }
        float max = (float) Math.max(Math.max(r, g), b);
        int avg = (int) (total / (pixels.length * 3));
        WhiteBalanceResult result = new WhiteBalanceResult();
        result.avg = avg;
        result.r = r / max;
        result.g = g / max;
        result.b = b / max;
        return result;
    }

    public static class WhiteBalanceResult {
        public float r;
        public float g;
        public float b;
        public int avg;
    }
}
